package com.furongji.education.controller.api;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.furongji.common.enums.CommentTargetTypeEnum;
import com.furongji.common.result.Result;
import com.furongji.education.domain.dto.CommentDto;
import com.furongji.education.domain.po.Comment;
import com.furongji.education.domain.vo.CommentVo;
import com.furongji.education.service.CommentService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 通用评论表 前端控制器
 * </p>
 *
 * @author furongji
 * @since 2025-06-03
 */

@Tag(name = "用户端评论管理")
@RequiredArgsConstructor
@RestController
@RequestMapping("/api/comments")
public class CommentApiController {

    private final CommentService commentService;

    @Operation(summary = "获取评论")
    @GetMapping("/getComments")
    public Result getComments(@RequestParam(defaultValue = "1") Integer current,
                              @RequestParam(defaultValue = "8") Integer size,
                              @RequestParam String targetId,
                              @RequestParam CommentTargetTypeEnum targetType) {
        IPage<CommentVo> commentVoList = commentService.getCommentsByTarget(current, size, targetId, targetType);
        return Result.success(commentVoList);
    }

    @Operation(summary = "追加评论")
    @PostMapping("/saveComment")
    public Result saveComment(@RequestBody CommentDto commentDto) throws JsonProcessingException {
        boolean flag = commentService.saveCommentByTarget(commentDto);
        return flag ? Result.success() : Result.error();
    }
}
